<template>
	<view class="ikz-bid">
		<ikz-loading :css-data="loading_css"></ikz-loading>
		<view class="ikz-top-bg">
			<image class="ikz-top-bg-img" :src="service_img"></image>
		</view>
		<view class="ikz-banner-box">
			<ikz-banner :show-data="banner_data" :css-data="banner_css"></ikz-banner>
			<view class="ikz-location" @click="getPosition">
				<image src="/static/index/location.png" class="ikz-location-img"></image>
				<view>
					{{default_agent.title}}
				</view>
			</view> 
		</view>
		<view class="ikz-notice">
			<ikz-notice :show-data="screen_data" @openScreen="openScreen"></ikz-notice>
		</view>
		<view> 
			<ikz-menu :show-data="menu_data" :css-data="menu_css"></ikz-menu>
		</view>
		<!-- ad lis  -> -->
		<view>
			<ikz-ad :show-data="ad_data" :css-data="ad_css"></ikz-ad>
		</view>  
		<!-- ad list
		<!-- 广告招商 -->
		<view class="ikz-ad" @click="goApply">
			<image class="ikz-ad-img" :src="apply_image"></image>
		</view>
		<!-- ad -->
		<!-- Tab切换 start-->
		<!-- <view class="ikz-tab">
			<view class="ikz-tab-item" :class="{ 'active': selectedTab === 1 }" @click="switchTab(1)">接单列表</view>
			<view class="ikz-tab-item" :class="{ 'active': selectedTab === 2 }" @click="switchTab(2)" v-if="wmshow == 1">商家超市</view>
			<view class="ikz-tab-item" :class="{ 'active': selectedTab === 3 }" @click="switchTab(3)" v-if="tbshow == 1">贴吧</view>
			<view class="ikz-tab-item" style="padding-right: 0;"><view style="width: 123rpx;height: 42rpx;"></view></view>
		</view> -->
		<!-- Tab切换 end-->  
		<!-- 接单列表 -->	
	<!-- 	<view v-if="listType == 1" style="padding-bottom: 32rpx;">
			<ikz-glist :show-type="type_list" :show-data="show_info" :more-status="load_more" :receiver="isReceiver"></ikz-glist>
		</view> -->
		<!-- 商家超市 -->
		<!-- <view v-else-if="listType == 2 && wmshow == 1" style="padding-bottom: 32rpx;">
			<store-list ref="storeList"></store-list>
		</view> -->
		<!-- 贴吧列表 -->
		<!-- <view v-else-if="listType == 3 && tbshow == 1">
			<mainList :show-data="postsList" @likePosts="likePosts"></mainList>
		</view> -->
		<!-- list -->
		<!-- <ikz-screen :show-data="screen_data" @closeScreen="closeScreen"></ikz-screen> -->
		<!-- 缺省 -->
		<view class="default-height" style="width: 100%;height: 92rpx;"></view>
		<ikz-tabbar :show-data="tabbar_Data"></ikz-tabbar>
		<!-- 悬浮图标联系客服 -->
		<!-- <view class="changeMode" @click="changeMode()">
			<image src="/static/kefu.png" mode="aspectFit"></image>
		</view>  -->
		<!-- 隐私授权弹窗 -->
		<!-- <privacy-popup position="center"></privacy-popup> -->
		
		<!-- 首次进来弹窗 -->
		<view class="poup_warp" v-if="isPoup" @click="close">
			<view class="image_w">
				<image  @click="close" :src="`${baseImage}/assets/img/poup.png`" mode=""></image>
				</view>
			</view>
			
	</view>
</template>

<script>
	// 将分包的组件复制到公共 
	import storeList from '@/pages/Component/Public/store/list.vue'
	import mainList from '@/pages/Component/Public/main-list/mainList.vue'
	import ikzBanner from '@/pages/Component/Public/Banner/public/banner/banner.vue';
	import ikzLoading from '@/pages/Component/Public/Loading/loading.vue';
	import ikzNotice from '@/pages/Component/Public/Notice/public/notice/notice.vue';
	import ikzMenu from '@/pages/Component/Public/Menu/public/menu2/menu.vue';
	import ikzAd from '@/pages/Component/Public/Ad/public/ad/ad.vue';
	import ikzGlist from '@/pages/Component/Public/Glist/public/glist/glist.vue';
	import ikzScreen from '@/pages/Component/Public/Screen/public/screen/screen.vue';
	import ikzTabbar from '@/pages/Component/Public/tab-bar/tab-bar.vue';
	var app = getApp();
	import ikz from '@/pages/Common/tools.js';
	import Vue from 'vue'
	export default {
		components: {
			ikzBanner,
			ikzLoading,
			ikzNotice,
			ikzMenu,
			ikzAd,
			ikzGlist,
			ikzScreen,
			ikzTabbar,
			storeList,
			mainList
		},
		data() {
			return {
				isPoup:true,
				baseImage:app.globalData.adminImgPath,
				// 超市外卖是否展示
				wmshow:0,
				// 校园贴吧是否展示
				tbshow:0,
				listType:1,
				selectedTab:1,
				postsList: [], //帖子列表
				tabbar_Data: {
					type: 0,
					list: []
				},
				show_info: {
					list: []
				},
				type_list: {
					list: []
				},
				screen_data: {

				},
				ad_data: {},
				ad_css: {},
				banner_data: {
					ban_page: 2
				},
				banner_css: {
					margin_top: 0
				},
				loading_css: {
					display: 'flex'
				},
				service_img: app.globalData.service_img,
				menu_data: {
					list: []
				},
				menu_css: {
					background_color: '#fff',
					bg_border_radius: 15,
					border_radius: '50%',
					color: '#888888',
					margin_top: 15,
					num: '4',
					width: 95
				},
				default_agent: {},
				apply_image: '',
				page: 1,
				limit: 10,
				load_more: 2,
				apply_receive_image_show: 1,
				// 是否为接单员  
				isReceiver: 0
			};
		},
		onLoad() {
			this.tabbar_Data.list = app.globalData.tabBarInfo
			uni.hideTabBar()
			const obj = wx.getEnterOptionsSync();
			if (obj.scene == 1047 || obj.scene == 1048 || obj.scene == 1049) {
				var pathId = {
					id: obj.query.scene
				}
				if (pathId.id) {
					uni.setStorageSync('default_agent', pathId);
				}
			}
			uni.setNavigationBarTitle({
				title: '跑腿猫'
			});
			this.page = 1;
			this.show_info = {
				list: []
			}
			this.type_list = {
				list: []
			}
			this.load_more = 2;
			this.listInfo((res) => {});
			this.homeInfo(res => {
				this.loading_css.display = 'none';
			});
			this.$forceUpdate();
			// uni.showShareMenu({ 
			//   withShareTicket: true,
			//   menus: ['shareAppMessage', 'shareTimeline']
			// }) 
			if(this.listType == 2){
				this.$refs.storeList.getStoreList()
			}else if(this.listType == 3){
				this.getPostsList()
			}
		},
		onShareTimeline() {
			return {
				title: '跑腿猫',
				query: '/page/Home/index?id=123'
			}
		},
		onShareAppMessage() {
			// const promise = new Promise(resolve => {
			//   setTimeout(() => {
			//     resolve({
			//       title: '自定义转发标题',
			//       imageUrl:'https://xybaddon.maiwd.cn/shareButton.png',
			//     })  
			//   }, 2000) 
			// })  
			return {
				title: app.globalData.share_info.share_title,
				path: '/pages/Home/index',
				imageUrl: app.globalData.share_info.share_img,
			}
		},
		onShow() {
			this.isPoup = uni.getStorageSync('isPoup')
			this.listInfo((res) => {});
			if (app.globalData.default_agent.title == undefined) {
				this.default_agent = {
					title: "请选择区域"
				};
			} else {
				this.default_agent = app.globalData.default_agent;
			}
			if(uni.getStorageSync('local_user_session')){
				this.getBaseinfo()
			}
			this.handleReceiver()
			if(this.listType == 2){
				this.$refs.storeList.getStoreList()
			}else if(this.listType == 3){
				this.getPostsList()
			} 
		},
		onPullDownRefresh() {
			this.homeInfo(res => {
				uni.stopPullDownRefresh();
			});
			this.page = 1;
			this.listInfo();
		},
		onReachBottom() {
			if (this.load_more == 2) {
				this.page++;
				this.load_more = 3;
				this.listInfo((res) => {});
			}
			if(this.listType == 2){
				this.$refs.storeList.onBottom()
			}
		},
		methods: {
			// 关闭弹窗
			close() {
				uni.setStorageSync('isPoup',false)
				this.isPoup = false
			},
			// 判断超市外卖和校园贴吧是否展示  
			judge(){
				console.log("menu_data的值是",this.menu_data);
				 // 初始化 tbshow 和 wmshow
				this.tbshow = 0;
				this.wmshow = 0;
				
				// 检查 menu_data.list 是否存在且为数组
				if (Array.isArray(this.menu_data.list)) {
					// 遍历 menu_data.list 数组
					this.menu_data.list.forEach(item => {
						if (item.title === "贴吧") {
							this.tbshow = 1; // 设置 tbshow 为 1
						} else if (item.title === "超市外卖") {
							this.wmshow = 1; // 设置 wmshow 为 1
						}
		 
						if (this.tbshow === 1 && this.wmshow === 1) {
						    return false; 
						}
		 
					});
				} 
				
			},
			switchTab(tab) {  
			  this.listType = tab;
			  this.selectedTab = tab;
			  this.$nextTick(() => {  
			     if (tab == 2) {
			         this.$refs.storeList.getStoreList();
			     } else if (tab == 3) {  
			         this.getPostsList();  
			     }       
			  });  
			},  
			// 获取最新或推荐帖子 
			getPostsList() { 
				let path = '/xyb/posts/posts/index'
				let data = {
					token: uni.getStorageSync('local_user_session'),
					recommend: 0,
					page: 1,
					limit: 10,
				}
				ikz.requestPost(path, data, res => {
					if (res.statusCode == 200) {
						console.log('获取最新或推荐帖子', res.data)
						this.postsList = []
						let postsList = []
						postsList = res.data.data.data
						postsList.forEach(el => {
							if (el.images.length != 0) {
								el.imagesNum = el.images.length
								if (el.images.length > 3) {
									el.images.slice(0, 3)
								}
							}
						})
						this.postsList = this.page == 1 ? postsList : [...this.postsList, ...postsList]
						this.hasMore = this.page < res.data.data.count / this.limit ? true : false
					}
					this.loadEnd = true
					uni.hideLoading()
					uni.stopPullDownRefresh()
				})
			},
			// 点赞或取消点赞帖子
			likePosts(id, status) {
				let path = ''
				if (status == 0) {
					path = '/xyb/posts/posts/give'
				} else {
					path = '/xyb/posts/posts/give_del'
				}
				let data = {
					token: uni.getStorageSync('local_user_session'),
					posts_id: id,
				}
				ikz.requestPost(path, data, res => {
					if (res.statusCode == 200) {
						// console.log(res.data.data);
						this.postsList.forEach(el => {
							if (el.id == id) {
								if (el.give_status == 0) {
									el.give_status = 1
									el.give++
								} else {
									el.give_status = 0
									el.give--
								}
							}
						})
						let title = ''
						if (status == 0) {
							title = '点赞成功'
						} else {
							title = '取消点赞'
						}
						uni.showToast({ 
							title,
							icon: 'none',
						})
					}
				})
			},
			//获取用户信息
			getBaseinfo() {
				var path = '/xyb/user/baseinfo';
				var data = {
					token: uni.getStorageSync('local_user_session'),
					app_plat: "MP-WEIXIN"
				};
				ikz.get(path, data, res => {
					if (res.data.code) {
						console.log("值是", res.data.data);
						var user_address = res.data.data.user_address_list
						if (user_address.length) {
							getApp().globalData.user_address.address_all = ''; //地址数据

							getApp().globalData.user_address.address_all = user_address; //地址数据
							for (var i = 0; i < user_address.length; i++) {
								if (user_address[i].defaults) {
									getApp().globalData.user_address.address_default = user_address[i]; //默认地址
									getApp().globalData.user_address.address_current = user_address[i]; //当前
								}
							}
							if (getApp().globalData.user_address.address_default == '') {
								getApp().globalData.user_address.address_default = user_address[0]; //默认地址
								getApp().globalData.user_address.address_current = user_address[0]; //当前
							}
						} else {
							getApp().globalData.user_address.address_all = ''; //地址数据
							getApp().globalData.user_address.address_default = ''; //默认地址
							getApp().globalData.user_address.address_current = ''; //当前
						}

						//设置用户信息
						if (res.data.data.user_info.nickname) {
							getApp().globalData.user_openinfo = res.data.data.user_info || {};
						} else {
							getApp().globalData.user_openinfo = {};
						}



					}

				});
			},
			//跳转申请接单员 
			goApply() {
				if (!uni.getStorageSync("user_info")) {
					uni.navigateTo({
						url: "/pages/login/index"
					})
					return
				}
				var path = '/xyb/user/auth';
				var data = {}
				ikz.post(path, data, res => {
					if (res.data.code == 1) {
						//-1已驳回，0未审核，1已通过，2未申请
						if (res.data.data.run_apply_status == 1) {
							uni.navigateTo({
								url: '/pages/Component/Module/Receiver/Program/home/index/index'
							})
						} else if (res.data.data.run_apply_status == 0) {
							uni.showModal({
								title: '提示',
								content: '已申请，无需重复申请'
							})
						} else if (res.data.data.run_apply_status == -1) {
							uni.navigateTo({
								url: '/pages/Component/Module/Receiver/Program/apply/applyfail/index'
							})
						} else {
							uni.navigateTo({
								url: '/pages/Component/Module/Receiver/Program/apply/index/index'
							})
						}
						this.$forceUpdate();
					} else {
						uni.showModal({
							title: '提示',
							content: res.data.msg,
							showCancel: false,
							success: function(res) {}
						});
					}
				});

			},
			//关闭公告
			closeScreen() {
				this.screen_data.show = false;
			},
			//打开公告
			openScreen() {
				this.screen_data.show = true
			},
			// 接单列表
			listInfo() {
				var path = '/xyb/mp_weixin_home/server_list';
				var url = app.globalData.adminPath + path;
				var data = {
					page: this.page,
					limit: this.limit,
					token: uni.getStorageSync('local_user_session'),
					agent_id: uni.getStorageSync('default_agent').id || ''
				}
				if (uni.getStorageSync('local_user_session')) {
					uni.request({
						url: url,
						header: {
							"Content-Type": "application/x-www-form-urlencoded"
						},
						data: data,
						method: 'POST',
						success: (res) => {
							if (res.data.code == 1) {
								var temp_list = res.data.data.data;
								if (this.page != 1) {
									for (var i = 0; i < temp_list.length; i++) {
										this.show_info.list.push(temp_list[i]);
									}
								} else {
									this.show_info.list = temp_list;
								}
								if (temp_list.length == this.limit) {
									this.load_more = 2;
								} else {
									if (this.page == 1) {
										if (temp_list.length == 0) {
											this.load_more = 5;
										} else {
											this.load_more = 4;
										}
									} else {
										this.load_more = 4;
									}
								}
								this.$forceUpdate();
							} else if (res.data.code == 0) {
								uni.showModal({
									title: '提示',
									content: res.data.msg,
									showCancel: false,
									success: function(res) {}
								});
							} else {
								console.log(res.data.msg)
							}
						},
						fail: function(res) {
							//调用接口失败或回传失败
							uni.showModal({
								title: '服务器繁忙',
								content: '服务器繁忙或网络不稳定，请稍后再试！'
							})
						},
					})
				}

			},
			// 判断是否是接单员 
			handleReceiver() {
				var path = '/xyb/user/is_receiver';
				var url = app.globalData.adminPath + path;
				var data = {
					app_plat: "MP-WEIXIN",
					token: uni.getStorageSync('local_user_session'),
					agent_id: uni.getStorageSync('default_agent').id || ''
				}
				if (data.token) {
					uni.request({
						url: url,
						header: {
							"Content-Type": "application/x-www-form-urlencoded"
						},
						data: data,
						method: 'POST',
						success: (res) => {
							console.log("是否为接单员", res.data.data.is_receiver);
							uni.setStorageSync('receivers', res.data.data.is_receiver)
							this.isReceiver = res.data.data.is_receiver
						},
						fail: function(res) {
							//调用接口失败或回传失败
							uni.showModal({
								title: '服务器繁忙',
								content: '服务器繁忙或网络不稳定，请稍后再试！'
							})
						},
					})
				}
			},
			//获取首页信息 
			homeInfo(fn) {
				var path = '/xyb/mp_weixin_home/index';
				var url = app.globalData.adminPath + path;
				var _that = this;
				uni.request({
					url: url,
					header: {
						"Content-Type": "application/x-www-form-urlencoded"
					},
					data: {
						"agent_id": uni.getStorageSync('default_agent').id || ''
					},
					method: 'POST',
					success: function(res) {
						if (res.data.code == 1) {
							_that.apply_receive_image_show = res.data.data.apply_receive_image_show;
							_that.ad_data = res.data.data.advertise_list;
							_that.banner_data = res.data.data.carousel_list;
							_that.apply_image = res.data.data.apply_receive_image;
							var menu_temp = res.data.data.server_list;
							_that.menu_data.list = []
							for (var i = 0; i < menu_temp.length; i++) {
								_that.menu_data.list.push({
									src: menu_temp[i].icon_image,
									title: menu_temp[i].server_name,
									url: menu_temp[i].url
								})
							}
							_that.screen_data = {
								title: res.data.data.notice_title,
								content: res.data.data.notice_content,
								show: false
							}
							_that.$forceUpdate();
							_that.judge()
						} else {
							uni.showModal({
								title: '提示',
								content: res.data.msg,
								showCancel: false,
								success: function(res) {}
							});
						}
						fn();
					},
					fail: function(res) {
						//调用接口失败或回传失败
						uni.showModal({
							title: '服务器繁忙',
							content: '服务器繁忙或网络不稳定，请稍后再试！'
						})
					},
				})
			},
			//切换位置
			getPosition() {
				var path = '/pages/Component/Module/Campus/Program/school/index/index';
				ikz.nav(path, 'n', '');
			},
			//不同意
			cancelTreaty() {
				//退出程序
				// plus.runtime.quit();
			},
			//查看平台协议
			getTreaty(e) {
				var id = e.currentTarget.dataset.id;
				var path = '/pages/Component/Module/Login/treaty/index?id=' + id;
				ikz.nav(path, 'n', '');
			},

			//请求页面数据
			getList() {

				var path = '/Supply/Wechat/Public/indexPage';
				var data = {
					longitude: this.longitude,
					latitude: this.latitude
				};
				ikz.get(path, data, res => {
					if (res.data.status == 'success') {

					} else {
						uni.showModal({
							title: '提示',
							content: '服务器繁忙，请稍后再试',
							showCancel: false,
							success: function(res) {}
						});
					}
					// if(that.pull_status){
					// 	this.$emit("change",'pull');
					// }
				});
			},

			//跳转至招聘中心
			getRecriut() {
				let url = '/pages/Component/Module/Demand/Program/Recruit/index';
				ikz.nav(url, 'n');
			},

		}
	};
</script>

<style scoped lang="scss">
	.poup_warp{
		position: fixed;
		top: 0;
		left: 0;
		width: 100vh;
		height: 100vh;
		z-index: 999999999;
		background-color: rgba(0, 0, 0, .6);
		.image_w{
			margin-left: 260rpx;
			margin-top: 30rpx;
			image{
				width: 476rpx;
				height: 546rpx;
			}
		}
		
	}
	.ikz-tab{
		padding:0 20rpx;
		display: flex;
		align-items: center;
		font-size: 32rpx;
		justify-content: space-between;
		
		.ikz-tab-item{
			padding-right: 16rpx;
			color: #333;
			
			&.active {  
			  color: #FED476; 
			  font-weight: 600;
			}  
		}
	}
	.ikz-top-bg {
		position: fixed;
		top: 0px;
		left: 0px;
	}

	.ikz-top-bg-img {
		width: 100%;
		height: 200rpx;
	}

	.ikz-banner-box {
		position: relative;
		width: 95%;
		margin: 15rpx auto 0px;

	}

	.ikz-location {
		box-sizing: border-box;
		position: absolute;
		top: 30rpx;
		left: 50rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 40rpx 10rpx 30rpx;
		background: rgba(000, 000, 000, 0.3);
		color: #fff;
		border-radius: 70rpx;
		font-size: 25rpx;


	}

	.ikz-location-img {
		width: 40rpx;
		height: 40rpx;
		margin-right: 10rpx;
	}

	/* ad */
	.ikz-ad {
		width: 95%;
		margin: 16rpx auto;
	}

	.ikz-ad-img {
		width: 100%;
		height: 150rpx;
	}

	.changeMode {
		position: fixed;
		right: 2.5%;
		bottom: 500rpx;
		padding-bottom: constant(safe-area-inset-bottom);
		/* 兼容 iOS < 11.2 */
		padding-bottom: env(safe-area-inset-bottom);
		/* 兼容 iOS >= 11.2 */
		z-index: 9999;
	}

	.changeMode image {
		width: 120rpx;
		height: 120rpx;
	}

	/* ad */
</style>